$(document).ready(function(){
	$(".ajax-get").click(function(){
		var target;
	    var that = this;
	    if($(this).hasClass('confirm')){
			if(!confirm("确定要执行该操作吗?")){
				return false;
			}
		}
	    if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {
	    	//alert(target);
	    	$.get(target,function(data){
	    		//console.log(data);
	    		if(data.flag == 0){
        			alert(data.content);
        		}else if(data.flag == 1){
        			updateAlert(data.content,true);
        			setTimeout(function(){
    					if(data.url)
    					{
    						location.href=data.url;
    					}
    					else{
    						location.reload();
    					}
    				},1500);
        		}else if(data.flag ==-1){
        			updateAlert(data.content);
        			setTimeout(function(){
        				if(data.url)
        				{
        					location.href=data.url;
        				}
        			},1500)
        		}
			},"json")
	    }
	    return false;
	});
	
	//ajax post submit请求
	$(".ajax-post").click(function(){
		var target,query,form;
        var target_form = $(this).attr('target-form');
        var that = this;
        if( ($(this).attr('type')=='button') || (target = $(this).attr('href')) || (target = $(this).attr('url')) ){
        	form = $('.'+target_form);
        	if(form.get(0).nodeName=='INPUT'||form.get(0).nodeName=='SELECT'||form.get(0).nodeName=='TEXTAREA')
			{
				form.each(function(k,v){
					if(v.type=="checkbox" && v.checked==true){
						nead_confirm = true;
					}
				})
				if(nead_confirm && $(this).hasClass('confirm')){
					if(!confirm('确认要执行该操作吗？')){
						return false;
					}
				}
				query = form.serialize();
			}else if(form.get(0).nodeName=='FORM')
			{
				if($(this).hasClass('confirm')){
					if(!confirm('确认要执行该操作吗？'))
					{
						return false;
					}
				}
				if($(this).attr('url') !== undefined){
					target = $(this).attr('url');
				}else{
					target = form.get(0).action;
				}
				query = form.serialize();
			}
        }
        $.post(target,query,function(data){
    		if(data.flag == 0){
    			alert(data.content);
    		}else if(data.flag == 1){
    			updateAlert(data.content,true);
    			setTimeout(function(){
					if(data.url)
					{
						location.href=data.url;
					}
					else{
						location.reload();
					}
				},1500);
    		}else if(data.flag ==-1){
    			updateAlert(data.content);
    		}
    	},"json");
	});
	
	
	//警告栏显示
	var am_alert = $('#top-alert');
	var alert_button = am_alert.find('button');
	alert_button.click(function(){
		am_alert.hide();
	});
	window.updateAlert = function(text,c)
	{
		text = text||'default';
		c = c||false;
		am_alert.css("display","block");
		if(text!='default'){
			am_alert.find('p').text(text);
		}
		else{
			am_alert.find('p').text('这是内容!');
		}
		if(c==false)
		{
			am_alert.removeClass("alert-success").addClass(' alert-error');
		}else{
			am_alert.removeClass("alert-error").addClass(" alert-success");
		}
	}
});